package com.xiao.admin.service.impl;

import com.xiao.admin.utils.JwtUtil;
import com.xiao.group.common.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.Query;

import com.xiao.admin.dao.UsersDao;
import com.xiao.admin.entity.UsersEntity;
import com.xiao.admin.service.UsersService;


@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersDao, UsersEntity> implements UsersService {

    @Autowired
    JwtUtil jwtUtil;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UsersEntity> page = this.page(
                new Query<UsersEntity>().getPage(params),
                new QueryWrapper<UsersEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R tologin(UsersEntity usersEntity) {
        UsersEntity selectOne = baseMapper.selectOne(new QueryWrapper<UsersEntity>().eq("username", usersEntity.getUsername()));
        if(StringUtils.isNotBlank(selectOne.getUsername())){
            String admin = jwtUtil.createJWT(selectOne.getUsername(), selectOne.getPassword(), "admin");
            return R.ok().put("data",selectOne).put("token",admin);
        }
        return R.error("登陆失败,请从新登陆");
    }

    @Override
    public UsersEntity tonlur(String username) {
        return baseMapper.selectOne(new QueryWrapper<UsersEntity>().eq("username",username));
    }

}